package com.wobito.common.utils;

import org.apache.commons.codec.digest.DigestUtils;

import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * @Author Wangbaba
 * @Date 2024/3/16 19:20
 * @Version 1.0
 */
public class MD5Utils {

    public static final String salt = "wbt1998";

    public static final String md5(String src) {
        return DigestUtils.md5Hex(src);
    }

    //加密
    public static String inputPassToFromPass(String inputPass) {
        String str = "" + salt.charAt(1) + salt.charAt(2) + inputPass + salt.charAt(4) + salt.charAt(6);
        return md5(str);
    }


    //接口加密鉴权 返回的是32位[小]
    public static String getMD5Hash(String input) {
        try {
            // 创建一个MD5消息摘要
            MessageDigest md = MessageDigest.getInstance("MD5");
            // 计算输入字符串的MD5哈希值
            byte[] messageDigest = md.digest(input.getBytes(StandardCharsets.UTF_8));
            // 将字节数组转换为十六进制字符串
            StringBuilder hexString = new StringBuilder();
            for (byte aMessageDigest : messageDigest) {
                String hex = Integer.toHexString(0xff & aMessageDigest);
                if (hex.length() == 1) hexString.append('0');
                hexString.append(hex);
            }

            return hexString.toString();
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        }
    }
}
